package com.ferry.service.impl;

import com.ferry.domain.Wall;
import com.ferry.domain.WallType;
import com.ferry.mapper.WallMapper;
import com.ferry.mapper.WallTypeMapper;
import com.ferry.service.WallService;
import com.ferry.utils.ImageUtils;
import com.ferry.utils.Response;
import com.ferry.utils.ResponseGenerator;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import io.minio.http.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;

@Service
public class WallServiceImpl implements WallService {

    @Autowired
    private WallMapper wallMapper;

    @Autowired
    private WallTypeMapper wallTypeMapper;

    @Autowired
    private ResponseGenerator generator;

    @Autowired
    private MinioClient minioClient;

    private static final String MINIO_BUCKET = "app";

    @Override
    public Response addWall(InputStream inputStream, String name, long size, Long typeId, int price) {
        WallType wallType = wallTypeMapper.selectById(typeId);
        if (wallType != null) {

//            Img from = Img.from(inputStream);
//            Image img = from.getImg();
//            byte[] srcImage = ImgUtil.toBytes(img, "png");
//            int width = img.getWidth(null);
//            int height = img.getHeight(null);
//            img = ImgUtil.cut(img, new Rectangle( (width/2)-400,(height/2)-300,800,600 ));
//
//            img = ImgUtil.pressText(img, "摆渡村版权所有", Color.WHITE
//                    , new Font("黑体", Font.BOLD, 20), 0, 0, 0.6f);
//            byte[] catImage = ImgUtil.toBytes(img, "png");

            int index = name.lastIndexOf('.');
            String suffix = name.substring(index + 1);

            try {
                // 原图
                byte[] image = ImageUtils.getImage(inputStream);

                BufferedImage image1 = ImageUtils.getImage(image);
                int width = image1.getWidth();
                int height = image1.getHeight();

                // 缩略图
                byte[] thumbnailsImage = ImageUtils.getThumbnailsImage(image, suffix, 1920, 1080);

                // 加水印的图
                byte[] bytes = ImageUtils.pressSign(image, "jpg", "摆渡村版权所有", 40);
                minioClient.putObject(PutObjectArgs.builder().
                        bucket(MINIO_BUCKET).object(name + "-image.jpg").
                        stream(new ByteArrayInputStream(image), image.length,0).build());
                minioClient.putObject(PutObjectArgs.builder().
                        bucket(MINIO_BUCKET).object(name + "-thumbnailsImage.jpg").
                        stream(new ByteArrayInputStream(thumbnailsImage), thumbnailsImage.length,0).build());
                minioClient.putObject(PutObjectArgs.builder().
                        bucket(MINIO_BUCKET).object(name + "-bytes.jpg").
                        stream(new ByteArrayInputStream(bytes), bytes.length,0).build());


                Wall wall = new Wall();
                wall.setWallName(name);
                wall.setWallHeight(height);
                wall.setWallWidth(width);
                wall.setWallSize(size);
                wall.setWallSort(0L);

                wall.setWallPath( minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().
                        bucket(MINIO_BUCKET).object(name + "-image.jpg").method(Method.GET).build()));
                wall.setWallSmallPath( minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().
                        bucket(MINIO_BUCKET).object(name + "-thumbnailsImage.jpg").method(Method.GET).build()));
                wall.setWallBQPath( minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().
                        bucket(MINIO_BUCKET).object(name + "-bytes.jpg").method(Method.GET).build()));
                wall.setWallTime(new Date());
                wall.setWallPrice(price);
                wall.setWallTypeId(wallType.getWallTypeId());
                wall.setWallTypeName(wallType.getWallTypeName());

                int insert = wallMapper.insert(wall);
                if (insert > 0) {
                    return generator.success("上传成功");
                } else {
                    return generator.fail("上传失败");
                }
            } catch (IOException e) {
                return generator.fail("系统错误");
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            return generator.fail("分类不存在");
        }
    }

    @Override
    public List<Wall> getAll() {
        return wallMapper.selectList(null);
    }

    @Override
    public boolean delete(Long id) {
        Wall wall = wallMapper.selectById(id);
        if (wall != null) {
            int i = wallMapper.deleteById(id);
            return i > 0;
        } else {
            return false;
        }
    }

    @Override
    public boolean addSort(Long id) {
        Wall wall = wallMapper.selectById(id);
        wall.setWallSort(wall.getWallSort() + 1);
        int i = wallMapper.updateById(wall);
        return i > 0;
    }

    @Override
    public boolean updateName(Long id, String name) {

        Wall wall = wallMapper.selectById(id);
        wall.setWallName(name);
        int i = wallMapper.updateById(wall);

        return i > 0;
    }

    public String getPath(String path) {
        return path.replace("rabbit/", "");
    }
}
